﻿#include "UserDetail_toEditUserDlg.h"
#include "UserDetail_toEditUserDlg.h"
#include "UserDetail_toEditUserDlg.h"



UserDetail_toEditUserDlg::UserDetail_toEditUserDlg(QWidget* parent)
	:QWidget(parent)
{
	this->setWindowTitle("编辑用户详细信息");
	this->resize(300, 200);		
	this->setWindowFlag(Qt::FramelessWindowHint);						//去掉边框和状态栏；
	QObject::installEventFilter(new eventFilterObject(this));			//自由拖动窗口
	this->setStyleSheet(R"(background-color:rgb(40,40,40);
									color:white;)");
	//this->setWindowFlag(Qt::WindowStaysOnTopHint);						//设置窗口置顶

	init();
}

UserDetail_toEditUserDlg::~UserDetail_toEditUserDlg()
{

}

void UserDetail_toEditUserDlg::init()
{
	m_user_id_edit = new QLineEdit;
	m_user_id_edit->setStyleSheet("border:none;background-color:white;color:black;");
	m_username_edit = new QLineEdit;
	m_username_edit->setStyleSheet("border:none;background-color:white;color:black;");
	m_mobile_edit = new QLineEdit;
	m_mobile_edit->setStyleSheet("border:none;background-color:white;color:black;");
	m_email_edit = new QLineEdit;
	m_email_edit->setStyleSheet("border:none;background-color:white;color:black;");
	m_group_gender = new QButtonGroup;
	m_gender_boy = new QRadioButton("男");
	m_gender_secret = new QRadioButton("保密");
	m_gender_gril = new QRadioButton("女");
	m_password_edit = new QLineEdit;
	m_password_edit->setStyleSheet("background-color:white;color:black");
	m_ok_btn = new QPushButton("ok");
	m_cancel_btn = new QPushButton("cancel");


	//总体布局：
	auto totality_layout = new QVBoxLayout(this);
	totality_layout->setSizeConstraint(QLayout::SetFixedSize);			//将布局的大小约束设置为固定大小，也就是布局的大小将被固定为一个固定的宽度和高度

	//窗口顶层的标题和关闭按钮部分的布局：
	{
		auto hlayout = new QHBoxLayout;

		//标题：
		auto title = new QLabel("编辑用户详细信息窗口");
		title->setStyleSheet("font:bold 13px");						//设置标题为加粗字体，再设置字体大小为13px；
		hlayout->addWidget(title);

		hlayout->addStretch();

		//关闭按钮：
		m_closeBtn = new QPushButton;
		m_closeBtn->setIcon(QIcon(":/ClientResource/close.png"));				//设置关闭按钮的图标
		hlayout->addWidget(m_closeBtn);

		//把顶层部分添加到总体布局上：
		totality_layout->addLayout(hlayout);
	}

	totality_layout->addSpacerItem(new QSpacerItem(1, 30));				//宽度为1，高度为30像素；


	//输入内容部分布局：
	auto form_layout = new QFormLayout;

	form_layout->addRow("<font color=red>*</font>用户id >", m_user_id_edit);
	form_layout->addRow(new QLabel("<font size=2 face='微软雅黑' color='gray'>此窗口该用户的id不可修改</font>"));
	form_layout->addRow("<font color=red>*</font>修改用户名 >", m_username_edit);
	form_layout->addRow(new QLabel("<font size=2 face='微软雅黑' color='gray'>最大长度64位，允许英文字母、数字、特殊字符</font>"));
	form_layout->addRow("<font color=red>*</font>用户密码不可展示 >", m_password_edit);
	form_layout->addRow(new QLabel("<font size=2 face='微软雅黑' color='gray'>此窗口该用户的密码不可修改</font>"));

	//性别部分布局：
	{
		auto hlayout = new QHBoxLayout;
		hlayout->addWidget(m_gender_boy);
		hlayout->addStretch();
		hlayout->addWidget(m_gender_secret);
		hlayout->addStretch();
		hlayout->addWidget(m_gender_gril);

		//添加到输入内容部分布局：
		form_layout->addRow(hlayout);
	}
	
	form_layout->addRow("<font color=red>*</font>修改电话号码 >", m_mobile_edit);
	form_layout->addRow(new QLabel("<font size=2 face='微软雅黑' color='gray'>最大长度11位，允许数字及-符号</font>"));
	form_layout->addRow("<font color=red>*</font>修改电子邮箱 >", m_email_edit);
	form_layout->addRow(new QLabel("<font size=2 face='微软雅黑' color='gray'>最大长度32位，允许英文字母、数字、@</font>"));

	//把输入内容部分布局添加到总体布局：
	totality_layout->addLayout(form_layout);

	//确认按钮部分布局：
	{
		auto hlayout = new QHBoxLayout;
		hlayout->addWidget(m_ok_btn);
		hlayout->addStretch();		//弹簧
		hlayout->addWidget(m_cancel_btn);

		//添加到总体布局：
		totality_layout->addLayout(hlayout);
	}

	//信号槽连接：
	connectinit();
}

void UserDetail_toEditUserDlg::setUserData(const QJsonObject& object)
{
	//获取用户数据：
	m_user = object;
	m_Newuser = object;


	//部署用户数据信息：
	updateUi();
}

void UserDetail_toEditUserDlg::updateUi()
{
	m_user_id_edit->setText(m_user.value("user_id").toString());
	m_user_id_edit->setEnabled(false);															//用户id不能被修改
	m_user_id_edit->setStyleSheet("background-color:white;color:gray");				//设置用户id为灰色
	m_username_edit->setText(m_user.value("username").toString());
	m_password_edit->setText("*******");
	m_password_edit->setEnabled(false);															//用户密码不能被修改
	m_password_edit->setStyleSheet("background-color:white;color:gray");				//设置用户密码为灰色
	auto gender = m_user.value("gender").toString();
	if (gender == "男")
	{
		m_gender_boy->setChecked(true);
	}
	else if (gender == "女")
	{
		m_gender_gril->setChecked(true);
	}
	else
	{
		m_gender_secret->setChecked(true);
	}
	m_mobile_edit->setText(m_user.value("mobile").toString());
	m_email_edit->setText(m_user.value("email").toString());
}

void UserDetail_toEditUserDlg::GetEditData()
{
	m_Newuser.insert("user_id", QString(m_user_id_edit->text()));
	m_Newuser.insert("username", QString(m_username_edit->text()));
	int gender{};
	if (m_gender_boy->isChecked())
	{
		gender = 1;
	}
	else if (m_gender_gril->isChecked())
	{
		gender = 2;
	}
	else
	{
		gender = 0;
	}
	m_Newuser.insert("gender", gender);
	m_Newuser.insert("mobile", QString(m_mobile_edit->text()));
	m_Newuser.insert("email", QString(m_email_edit->text()));
}

void UserDetail_toEditUserDlg::connectinit()
{
	//设置性别组：
	m_group_gender->addButton(m_gender_boy);
	m_group_gender->addButton(m_gender_secret);
	m_group_gender->addButton(m_gender_gril);


	//关闭按钮，关闭窗口:
	connect(m_closeBtn, &QPushButton::clicked, this, [=]
		{
			emit windowclose();
			this->close();
		});

	//确认按钮：
	connect(m_ok_btn, &QPushButton::released, this,&UserDetail_toEditUserDlg::on_OKBtnslot);

	//取消按钮，关闭该窗口：
	connect(m_cancel_btn, &QPushButton::clicked, this, [=]()
		{
			//获取编辑框上的数据：
			GetEditData();

			//如果和原始数据不同，就要弹框提示一下：
			if (m_user != m_Newuser)
			{
				if (QMessageBox::Cancel == QMessageBox::question(0, QObject::tr("tip~~~"), "啊？你还真改啊？ 那你到底要不要考虑下还退出不？", QMessageBox::Yes | QMessageBox::Cancel))
				{
					return;
				}
			}

			m_Newuser = QJsonObject();

			emit windowclose();
			this->close();
		});

}

void UserDetail_toEditUserDlg::on_OKBtnslot()
{
	//获取修改后的数据：
	GetEditData();

	//如果数据没有修改：
	if (m_user == m_Newuser)
	{
		//清空对象：
		m_Newuser = QJsonObject();

		//如果数据没有被修改，那么按下ok键也是返回:
		emit windowclose();
		this->close();
		return;
	}

	//数据修改提示:
	if (QMessageBox::Cancel == QMessageBox::question(0, QObject::tr("tip~~~"), "确定修改该用户的数据么", QMessageBox::Yes | QMessageBox::Cancel))
	{
		emit windowclose();
		this->close();
		return;
	}


	SHttpClient(QUrl("http://" + sApp->globalConfig()->value("host/host").toString() + QString("/api/user/modify?user_id=%1").arg(m_user.value("user_id").toString())))
		.debug(true)
			.header("Authorization", "Bearer" + sApp->userData("user/token").toString())
				.json(m_Newuser)
					.success([=](const QByteArray& data)
						{
							QJsonParseError error;
							auto jdom = QJsonDocument::fromJson(data, &error);
							if (error.error != QJsonParseError::NoError && jdom["code"].toInt() > 1000) {
								return;
							}
							else {
								emit userDataChanged(m_Newuser);
								this->close();
							}
						})
						.post();


	this->close();
}

void UserDetail_toEditUserDlg::keyPressEvent(QKeyEvent* ev)
{
	if (ev->type() == Qt::Key_Return)
	{
		on_OKBtnslot();
	}

	// 调用父类的 keyPressEvent 函数以确保其他事件被正确处理
	QWidget::keyPressEvent(ev);
}

